package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.logic.pb.CrossUnionPb;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

@CmdApi(reqMsg = UnionProto.UnionChamberDispatchReqMsg.class, respMsg = UnionProto.UnionChamberDispatchRespMsg.class)
@Cmd(code = Protocol.S_UNION_CHAMBER_DISPATCH, desc = "商会议事厅派遣")
public class UnionChamberDispatchCmd extends AbstractCommand {

	private int patronsId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		UnionProto.UnionChamberDispatchReqMsg reqMsg = UnionProto.UnionChamberDispatchReqMsg.parseFrom(packet.getBytes());
		patronsId = reqMsg.getPatronsId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(patronsId < 0){
			ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
			return;
		}
		if(!SystemOpenMgr.systemOpen(player, eSystemId.Union.getValue())){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}

		if (StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())) {
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}

		UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(player.getUserInfo().getUnionUid());
		if(null == unionInfo || unionInfo.isDelete()){
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}

		UserPatrons userPatrons = null;
		if(patronsId > 0) {
			userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
			if (null == userPatrons) {
				ret = GameErrorCode.E_PATRONS_NO_FOUND;
				return;
			}
		}

		UnionMember unionMember = CrossUnionMgr.getUnionMember(player.getUserInfo().getUnionUid(), player.getUserId());
		if(null == unionMember){
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}

		CrossUnionProto.UnionChamberDispatchCrossReqMsg.Builder crossReq = CrossUnionProto.UnionChamberDispatchCrossReqMsg.newBuilder();
		if(userPatrons != null) {
			crossReq.setPatrons(CrossUnionPb.buildCrossUnionChamberPatronsTemp(userPatrons));
		}
		crossReq.setPatronsId(patronsId);
		crossReq.setNeedBack(true);
		player.sendPacket(Protocol.C_UNION_CHAMBER_DISPATCH, crossReq);

	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0) {
			UnionProto.UnionChamberDispatchRespMsg.Builder respMsg = UnionProto.UnionChamberDispatchRespMsg.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_UNION_CHAMBER_DISPATCH, respMsg);
		}
//		if(isNotify){
//			UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
//			UnionMgr.notifyAllMemberChamberChange(player.getUserInfo().getUnionUid(), player.getUserId(), false, userPatrons);
//		}
	}

}
